package cc.rengu.redp.flowable.bizmodel.metadata;

import cc.rengu.redp.flowable.bizmodel.db.domain.BizCfgModel;
import cc.rengu.redp.flowable.bizmodel.db.domain.BizIndex;
import cc.rengu.redp.flowable.bizmodel.web.service.BizIndexService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class BizDataIndexHandler implements IndexHandler {
    @Autowired
    private BizIndexService bizIndexService;

    @Override
    public void create(Long bizDataId, JSONObject srcJsonObj, BizCfgModel bizCfgModel) {
        bizCfgModel.getFieldData().forEach(bizCfgField ->{
            String fieldValue = srcJsonObj.getString(bizCfgField.getFieldName());
            if (bizCfgField.getIsIndex() && StringUtils.isNotBlank(fieldValue)){
                BizIndex bizIndex = new BizIndex();
                bizIndex.setModelId(bizCfgModel.getModelId());
                bizIndex.setDataId(bizDataId);
                bizIndex.setFieldName(bizCfgField.getFieldName());
                if ("String".equals(bizCfgField.getFieldType())){
                    bizIndex.setFieldValueString(fieldValue);
                    bizIndexService.save(bizIndex);
                } else if ("Date".equals(bizCfgField.getFieldType())) {
                    bizIndex.setFieldValueDate(LocalDate.parse(fieldValue));
                    bizIndexService.save(bizIndex);
                }
            }
        });
    }

    @Override
    public void update(Long bizDataId, JSONObject srcJsonObj, BizCfgModel bizCfgModel) {
        bizCfgModel.getFieldData().forEach(bizCfgField -> {
            String fieldValue = srcJsonObj.getString(bizCfgField.getFieldName());
            if (bizCfgField.getIsIndex() && StringUtils.isNotBlank(fieldValue)) {
                LambdaQueryWrapper<BizIndex> lqw = new LambdaQueryWrapper<>();
                lqw.eq(BizIndex::getDataId, bizDataId).eq(BizIndex::getFieldName, bizCfgField.getFieldName());
                BizIndex bizIndex = bizIndexService.getBaseMapper().selectOne(lqw);

                LambdaUpdateWrapper<BizIndex> luw = new LambdaUpdateWrapper<>();
                luw.eq(BizIndex::getDataId, bizIndex.getDataId()).eq(BizIndex::getFieldName, bizCfgField.getFieldName());
                if ("String".equals(bizCfgField.getFieldType()) && !fieldValue.equals(bizIndex.getFieldValueString())) {
                    luw.set(BizIndex::getFieldValueString, fieldValue);
                    bizIndexService.update(luw);
                } else if ("Date".equals(bizCfgField.getFieldType()) && !LocalDate.parse(fieldValue).equals(bizIndex.getFieldValueDate())) {
                    luw.set(BizIndex::getFieldValueDate, LocalDate.parse(fieldValue));
                    bizIndexService.update(luw);
                }
            }
        });
    }
}
